package com.fingard.dsp.bankset;

import java.io.File;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.DSPSet;
import com.fingard.dsp.XmlHander;
import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.core.DSPSecurityData;
import com.fingard.dsp.entity.enums.ENDirectInfoType;
import com.fingard.dsp.entity.vo.BankfrontDetailsVO;
import com.fingard.io.FileHelper;
import com.fingard.dsp.core.dao.bankfront.BankfrontDetailsMapper;
import com.fingard.luna.framework.boot.core.util.SpringContextHolder;
import com.fingard.text.StringHelper;
import com.fingard.util.SortHelper;
import com.fingard.xml.XmlTextReader;

public class BankFrontCenter {
    public HashMap<String, BankFrontBalance> hashMapBankFront = null;

    public BankFrontCenter(DSPSet p_dspSet) throws Exception {
        hashMapBankFront = new HashMap<String, BankFrontBalance>();
        uridMapGroup = new HashMap<String, String>();
        codeMapUrid = new HashMap<String, String>();
        refreshBankFront(p_dspSet, "", "");
    }

    private Object lockMutex = new Object();

    private HashMap<String, String> uridMapGroup = null;
    private HashMap<String, String> codeMapUrid = null;

    private void removeFromMap(String urid, String code) {
        if (uridMapGroup.containsKey(urid)) {
            uridMapGroup.remove(urid);
        }
        if (codeMapUrid.containsKey(code)) {
            codeMapUrid.remove(code);
        }
    }

    public String refreshBankFront(DSPSet p_dspSet, String refreshType, String refreshKey) throws Exception {
        synchronized (lockMutex) {//同步代码
            return refreshBankFrontInner(p_dspSet, refreshType, refreshKey);
        }
    }

    public BankFront getBankFrontByUrid(String pUrid) {
        if (uridMapGroup.containsKey(pUrid)) {
            String findGroup = uridMapGroup.get(pUrid);
            BankFrontBalance bfbal = hashMapBankFront.get(findGroup);
            if (bfbal != null) {
                return bfbal.getBankFrontByURID(pUrid);
            }
        }
        return null;
    }

    /**
     * 重置前置机会话token
     */
    public String refreshBankFrontToken(DSPSet p_dspSet, String refreshType, String refreshKey) throws Exception {
        String retValue = "";

        String findUrid = "";
        if ("ByURID".equalsIgnoreCase(refreshType)) {
            findUrid = refreshKey;
        } else if ("ByCode".equalsIgnoreCase(refreshType)) {
            if (codeMapUrid.containsKey(refreshKey)) {
                findUrid = codeMapUrid.get(refreshKey);
            }
        }
        BankFront findBf = getBankFrontByUrid(findUrid);
        if (findBf != null) {
            String oldToken = findBf.token;
            findBf.token = "";
            retValue = "已重置会话token（旧值为“" + oldToken + "”）：" + findBf.urid + "-" + findBf.code + "-" + findBf.name;
        } else {
            retValue = "未能根据" + findUrid + "找到前置机；\r\n";
        }

        return retValue;
    }


    /**
     * 从数据库中读取前置机的配置信息
     *
     * @return
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    private void getBankfrontFromDB() throws IllegalAccessException, NoSuchFieldException {

        BankfrontDetailsMapper bankfrontDetailsMapper;
        List<BankfrontDetailsVO> bankfrontDetailsPOS;
        try {
            bankfrontDetailsMapper = SpringContextHolder.getBean(BankfrontDetailsMapper.class);
            bankfrontDetailsPOS = bankfrontDetailsMapper.selectBankfrontFromDB();
        } catch (Exception ex) {
            DSPSecurityData.getInstance().setReadFileFlag(true);
            return;
        }

        if (bankfrontDetailsPOS == null || bankfrontDetailsPOS.size() == 0) {
            return;
        }
        Map<String, BankFront> bankFrontMap = new HashMap<>();
        for (BankfrontDetailsVO detailsPO : bankfrontDetailsPOS) {
            BankFront bankFront = bankFrontMap.get(detailsPO.getParentid());
            if (bankFront == null) {
                bankFront = new BankFront();
                if (StringHelper.isNullOrEmpty(bankFront.bankCode)) {
                    bankFront.bankCode = detailsPO.getDirectchannelCode();
                }
                if (StringHelper.isNullOrEmpty(bankFront.urid)) {
                    bankFront.urid = detailsPO.getParentid();
                }
                if (StringHelper.isNullOrEmpty(bankFront.name)) {
                    bankFront.name = detailsPO.getName();
                }
                if (StringHelper.isNullOrEmpty(bankFront.corporation)) {
                    bankFront.corporation = detailsPO.getCorporation();
                }
                if (StringHelper.isNullOrEmpty(bankFront.group)) {
                    bankFront.group = detailsPO.getGroupCode();
                }
                if (StringHelper.isNullOrEmpty(bankFront.tenant)) {
                    bankFront.tenant = detailsPO.getTenantId();
                }
                if (StringHelper.isNullOrEmpty(bankFront.code)) {
                    bankFront.code = detailsPO.getCode();
                }
                bankFrontMap.put(detailsPO.getParentid(), bankFront);
            }
            String infotype = detailsPO.getInfotype();
            //定时任务的配置
            if (ENDirectInfoType.TASK.value().equals(infotype)) {
                bankFront.hashTimerSet = new HashMap<>();
                TaskTimerSet taskTimerSet = new TaskTimerSet();
                String pushreceiptTime = detailsPO.getBankfrontDetailValue();
                if (Integer.valueOf(pushreceiptTime) > 0) {
                    taskTimerSet.enable = true;
                    taskTimerSet.intervalSecs = Long.parseLong(pushreceiptTime);
                    taskTimerSet.taskNameList = detailsPO.getFieldname();
                    taskTimerSet.bankFront = bankFront;
                    taskTimerSet.taskParams = new HashMap<>(0);
                    bankFront.hashTimerSet.put("", taskTimerSet);
                }
                continue;
            }
            Field field = bankFront.getClass().getField(detailsPO.getFieldname());
            field.set(bankFront, detailsPO.getBankfrontDetailValue());
        }

        for (String key : bankFrontMap.keySet()) {
            BankFront bankFront = bankFrontMap.get(key);
            String tmpMapKey = bankFront.bankCode + "-" + bankFront.corporation + "-" + bankFront.group + "-" + bankFront.tenant;
            loadBankfront(tmpMapKey, bankFront, "", false);
        }
    }

    /**
     * 刷新前置缓存，refreshType为空则为初次加载
     */
    private String refreshBankFrontInner(DSPSet p_dspSet, String refreshType, String refreshKey) throws Exception {
        String retValue = "";
        String findUrid = "";
        String findGroup = "";
        boolean isAdd = false;//是新增
        boolean isExists = true;//在刷新的情况下，是否还存在

        if (StringHelper.hasAnyChar(refreshType)) {
            isExists = false;
            if (refreshType.equals("ByURID")) {
                findUrid = refreshKey;
            } else if (refreshType.equals("ByCode")) {
                if (codeMapUrid.containsKey(refreshKey)) {
                    findUrid = codeMapUrid.get(refreshKey);
                } else {
                    isAdd = true;
                }
            }

            if (uridMapGroup.containsKey(findUrid)) {
                findGroup = uridMapGroup.get(findUrid);
            } else {
                isAdd = true;
            }
        }

        if (!p_dspSet.useDBConn) {
            getBankfrontFromDB();
            String tmpFileName = p_dspSet.rootDirPath + File.separator + "config" + File.separator + "BankFront.xml";
            String strBankFront = FileHelper.readFileText(tmpFileName, "utf-8");
            if (strBankFront == null && p_dspSet.dbConnectionSet != null) {
                strBankFront = XmlHander.dbtoxml("BankFront.xml", p_dspSet.dbConnectionSet.getConnection(), "config");
            }
            XmlTextReader xmlReader = new XmlTextReader(strBankFront);
            int tmpID = 100;
            while (xmlReader.read("/config/bankFront")) {
                //Element tmpEachElement = (Element) tmpNodeList.item(i);
                BankFront tmpBankFront = new BankFront();
                tmpBankFront.fromXElement(xmlReader.readedXElement);
                tmpBankFront.decrypt(xmlReader.readedXElement);
                tmpBankFront.setDecrypt();
                if (StringHelper.isNullOrEmpty(tmpBankFront.urid)) {
                    tmpBankFront.urid = String.valueOf(tmpID++);
                }

                String[] entities = tmpBankFront.corporation.split(",");

                for (String entity : entities) {

                    //租户默认配置
                    String tenant = tmpBankFront.tenant;
                    if (tenant == null || tenant.length() == 0) {
                        if ("true".equals(p_dspSet.disableTenant)) {
                            tenant = p_dspSet.tenant;
                        } else if (!"false".equals(p_dspSet.disableTenant)) {
                            String[] tenantArray = p_dspSet.disableTenant.split(",");
                            for (int i = 0, length = tenantArray.length; i < length; i++) {
                                if (tmpBankFront.bankCode.equals(tenantArray[i])) {
                                    tenant = p_dspSet.tenant;
                                }
                            }
                        }
                    }
                    tmpBankFront.tenant = tenant;
                    String tmpMapKey = tmpBankFront.bankCode + "-" + entity + "-" + tmpBankFront.group + "-" + tenant;
                    if (hashMapBankFront.containsKey(tmpMapKey)) {
                        continue;
                    }
                    if (StringHelper.isNullOrEmpty(refreshType) || isAdd) {
                        String returnResult = loadBankfront(tmpMapKey, tmpBankFront, retValue, isAdd);
                        if (!StringHelper.isNullOrEmpty(returnResult)) {
                            return returnResult;
                        }
                    } else {
                        if (findUrid.equals(tmpBankFront.urid)) {//是需要刷新的前置机
                            BankFront findBf = hashMapBankFront.get(findGroup).getBankFrontByURID(findUrid);
                            if (findBf == null) {
                                retValue += "未能找到已加载的前置机，URID：" + findUrid + "，请重新刷新；\r\n";
                                removeFromMap(findUrid, tmpBankFront.code);
                                return retValue;
                            }

                            findBf.fromXElement(xmlReader.readedXElement);
                            if (StringHelper.isNullOrEmpty(findBf.urid)) {
                                findBf.urid = findUrid;
                            }
                            findBf.decrypt(xmlReader.readedXElement);
                            findBf.setDecrypt();

                            retValue += "已刷新前置机：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "；\r\n";
                            return retValue;

                            /*
                            if (tmpMapKey.equals(findGroup)) {//分组没变化
                                retValue += "已刷新前置机：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "：" + tmpMapKey + "；\r\n";
                                return retValue;
                            } else {//分组有变化，需要转移
                                if (hashMapBankFront.containsKey(tmpMapKey) == false) {
                                    hashMapBankFront.put(tmpMapKey, new BankFrontBalance());
                                }
                                hashMapBankFront.get(tmpMapKey).listBankFront.add(findBf);
                                codeMapUrid.put(findBf.code, findBf.urid);
                                uridMapGroup.put(findBf.urid, tmpMapKey);

                                hashMapBankFront.get(findGroup).removeBankFrontByURID(findUrid);

                                retValue += "已刷新前置机及重新分组：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "：" + findGroup + "调整为" + tmpMapKey + "；\r\n";
                                findGroup = tmpMapKey;
                                return retValue;
                            }
                            */
                        }
                    }
                }
            }
        } else {

            Connection tmpConn = p_dspSet.dbConnectionSet.getConnection();
            PreparedStatement pre = null;
            try {
                pre = tmpConn.prepareStatement(BankFront.dbSelectSql);
                ResultSet rs = pre.executeQuery();
                while (rs.next()) {
                    BankFront tmpBankFront = new BankFront();
                    tmpBankFront.fromResultSet(rs);
                    String tmpMapKey = tmpBankFront.bankCode + "-" + tmpBankFront.corporation + "-" + tmpBankFront.group;

                    if (StringHelper.isNullOrEmpty(refreshType) || isAdd) {
                        if (hashMapBankFront.containsKey(tmpMapKey) == false) {
                            hashMapBankFront.put(tmpMapKey, new BankFrontBalance());
                        }
                        BankFront existBf = hashMapBankFront.get(tmpMapKey).getBankFrontByURID(tmpBankFront.urid);
                        if (existBf == null) {
                            hashMapBankFront.get(tmpMapKey).listBankFront.add(tmpBankFront);

                            codeMapUrid.put(tmpBankFront.code, tmpBankFront.urid);
                            uridMapGroup.put(tmpBankFront.urid, tmpMapKey);
                            String addMsg = "已加载前置机：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "：" + tmpMapKey;
                            retValue += "；\r\n";
                            isExists = true;
                            FGBiz.startLog.addLogNoTime(addMsg);
                        } else {
                            for (Map.Entry<String, Map<String, String>> entry : tmpBankFront.frontSet.entrySet()) {
                                if (existBf.frontSet.containsKey(entry.getKey())) {
                                    existBf.frontSet.get(entry.getKey()).putAll(entry.getValue());
                                } else {
                                    existBf.frontSet.put(entry.getKey(), entry.getValue());
                                }
                            }
                            for (String key : tmpBankFront.frontSet.keySet()) {
                                if (existBf.frontSet.containsKey(key)) {
                                    existBf.frontSet.get(key).putAll(tmpBankFront.frontSet.get(key));
                                } else {
                                    existBf.frontSet.put(key, tmpBankFront.frontSet.get(key));
                                }
                            }
                        }
                    } else {
                        if (findUrid.equals(tmpBankFront.urid)) {//是需要刷新的前置机
                            BankFront findBf = hashMapBankFront.get(findGroup).getBankFrontByURID(findUrid);
                            if (findBf == null) {
                                retValue += "未能找到已加载的前置机，URID：" + findUrid + "，请重新刷新；\r\n";
                                removeFromMap(findUrid, tmpBankFront.code);
                                return retValue;
                            }
                            if (isExists == false) {//第一次
                                findBf.fromResultSet(rs);
                                if (tmpMapKey.equals(findGroup)) {//分组没变化
                                    retValue += "已刷新前置机：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "：" + tmpMapKey + "；\r\n";
                                } else {//分组有变化，需要转移
                                    if (hashMapBankFront.containsKey(tmpMapKey) == false) {
                                        hashMapBankFront.put(tmpMapKey, new BankFrontBalance());
                                    }
                                    hashMapBankFront.get(tmpMapKey).listBankFront.add(findBf);
                                    codeMapUrid.put(findBf.code, findBf.urid);
                                    uridMapGroup.put(findBf.urid, tmpMapKey);
                                    hashMapBankFront.get(findGroup).removeBankFrontByURID(findUrid);
                                    retValue += "已刷新前置机及重新分组：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "：" + findGroup + "调整为" + tmpMapKey + "；\r\n";
                                    findGroup = tmpMapKey;
                                }
                            } else {
                                for (Map.Entry<String, Map<String, String>> entry : tmpBankFront.frontSet.entrySet()) {
                                    if (findBf.frontSet.containsKey(entry.getKey())) {
                                        findBf.frontSet.get(entry.getKey()).putAll(entry.getValue());
                                    } else {
                                        findBf.frontSet.put(entry.getKey(), entry.getValue());
                                    }
                                }
                                for (String key : tmpBankFront.frontSet.keySet()) {
                                    if (findBf.frontSet.containsKey(key)) {
                                        findBf.frontSet.get(key).putAll(tmpBankFront.frontSet.get(key));
                                    } else {
                                        findBf.frontSet.put(key, tmpBankFront.frontSet.get(key));
                                    }
                                }
                            }
                            isExists = true;
                        }
                    }
                }
                if (StringHelper.isNullOrEmpty(refreshType)) {
                    for (BankFrontBalance bfBalance : hashMapBankFront.values()) {
                        for (BankFront tmpBankFront : bfBalance.listBankFront) {
                            tmpBankFront.compatible();
                        }
                    }
                } else if (isExists) {
                    BankFront findBf = hashMapBankFront.get(findGroup).getBankFrontByURID(findUrid);
                    if (findBf != null) {
                        findBf.compatible();
                    }
                }
            } catch (Exception ex) {
                throw new Exception(ex);
            } finally {
                if (pre != null)
                    pre.close();
                if (tmpConn != null) {
                    tmpConn.close();
                }
            }
        }
        if (StringHelper.hasAnyChar(refreshType) && !isAdd && !isExists) {//删除
            if (hashMapBankFront.get(findGroup) != null) {
                BankFront bf = hashMapBankFront.get(findGroup).getBankFrontByURID(findUrid);
                if (bf != null) {
                    retValue += "已移除前置机，URID：" + findUrid + "-" + bf.code + "-" + bf.name + "；\r\n";
                    removeFromMap(bf.urid, bf.code);
                    hashMapBankFront.get(findGroup).removeBankFrontByURID(findUrid);
                } else {
                    retValue += "未能找到需要移除的前置机，URID：" + findUrid + "；\r\n";
                }
            } else {
                retValue += "未能找到需要移除的前置机分组，mapKey：" + findGroup + "；\r\n";
            }
        }
        return retValue;
    }

    /**
     * 加载前置机配置进内存
     *
     * @param tmpMapKey
     * @param tmpBankFront
     * @param retValue
     * @param isAdd
     */
    private String loadBankfront(String tmpMapKey, BankFront tmpBankFront, String retValue, boolean isAdd) {
        if (!hashMapBankFront.containsKey(tmpMapKey)) {
            hashMapBankFront.put(tmpMapKey, new BankFrontBalance());
        }
        hashMapBankFront.get(tmpMapKey).listBankFront.add(tmpBankFront);
        codeMapUrid.put(tmpBankFront.code, tmpBankFront.urid);
        uridMapGroup.put(tmpBankFront.urid, tmpMapKey);
        retValue += "；\r\n";
        if (isAdd) {
            return retValue;
        }
        if ("true".equals(tmpBankFront.useDirectchannelJarFile)) {
            FGBiz.startLog.addLogNoTime("**使用直连渠道升级jar包** " + "已加载前置机：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "：" + tmpMapKey);
        } else {
            FGBiz.startLog.addLogNoTime("已加载前置机：" + tmpBankFront.urid + "-" + tmpBankFront.code + "-" + tmpBankFront.name + "：" + tmpMapKey);
        }
        return "";
    }

    public String getRuntimeJsonList() {
        StringBuilder sbJson = new StringBuilder();
        sbJson.append("{");
        sbJson.append("\"respCode\":" + "\"0\",\r\n");
        sbJson.append("\"respInfo\":" + "\"\",\r\n");

        sbJson.append("\"columns\":[");
        String[] columns = new String[]{"urid", "code", "name", "directSys", "payConcurrent", "qryConcurrent", "eleConcurrent", "eleQryConcurrent", "lastExecPayTime", "lastReleasePayTime", "lastExecPayQryTime", "lastReleasePayQryTime", "lastExecOtherTime", "lastReleaseOtherTime", "corporation", "group", "tenant"};
        for (int i = 0; i < columns.length; i++) {
            if (i > 0) {
                sbJson.append(",");
            }
            sbJson.append("\"" + columns[i] + "\"");
        }
        sbJson.append("],\r\n");

        sbJson.append("\"dataList\":[");
        ArrayList<String> listSubJson = new ArrayList<String>();
        ArrayList<Integer> listOrderNo = new ArrayList<Integer>();

        Iterator<String> iter = hashMapBankFront.keySet().iterator();
        while (iter.hasNext()) {
            String eachKey = iter.next();
            for (BankFront eachFront : hashMapBankFront.get(eachKey).listBankFront) {
                int payConcurrent = eachFront.getPayConcurrent();
                int qryConcurrent = eachFront.getQryConcurrent();
                int eleConcurrent = eachFront.getEleConcurrent();
                int eleQryConcurrent = eachFront.getEleQryConcurrent();
                int total4order = payConcurrent + qryConcurrent + eleConcurrent + eleQryConcurrent;

                if (total4order < 1000) {
                    total4order = total4order * 1000;
                }
                String urid4order = eachFront.urid;
                if (urid4order.length() > 3) {
                    urid4order = urid4order.substring(0, 3);
                }
                if (StringHelper.isDigit(urid4order)) {
                    total4order += 1000 - Integer.parseInt(urid4order);
                }
                listOrderNo.add(total4order);

                StringBuilder sbSub = new StringBuilder();
                sbSub.append("{");
                sbSub.append("\"urid\":\"" + eachFront.urid.trim() + "\"");
                sbSub.append(",\"code\":\"" + eachFront.code.trim() + "\"");
                sbSub.append(",\"name\":\"" + eachFront.name.trim() + "\"");
                sbSub.append(",\"directSys\":\"" + eachFront.bankCode.trim() + "\"");

                sbSub.append(",\"payConcurrent\":\"" + payConcurrent + "/" + eachFront.frontPayMaxCcCount + "\"");
                sbSub.append(",\"qryConcurrent\":\"" + qryConcurrent + "/" + eachFront.frontQueryMaxCcCount + "\"");

                sbSub.append(",\"eleConcurrent\":\"" + eleConcurrent + "/" + eachFront.frontEleMaxCcCount + "\"");
                sbSub.append(",\"eleQryConcurrent\":\"" + eleQryConcurrent + "/" + eachFront.frontEleQueryMaxCcCount + "\"");

                if (eachFront.lastExecPayTime != null) {
                    sbSub.append(",\"lastExecPayTime\":\"" + Format.DateTimeFormat.format(eachFront.lastExecPayTime) + "\"");
                } else {
                    sbSub.append(",\"lastExecPayTime\":\"\"");
                }
                if (eachFront.lastReleasePayTime != null) {
                    sbSub.append(",\"lastReleasePayTime\":\"" + Format.DateTimeFormat.format(eachFront.lastReleasePayTime) + "\"");
                } else {
                    sbSub.append(",\"lastReleasePayTime\":\"\"");
                }

                if (eachFront.lastExecPayQryTime != null) {
                    sbSub.append(",\"lastExecPayQryTime\":\"" + Format.DateTimeFormat.format(eachFront.lastExecPayQryTime) + "\"");
                } else {
                    sbSub.append(",\"lastExecPayQryTime\":\"\"");
                }
                if (eachFront.lastReleasePayQryTime != null) {
                    sbSub.append(",\"lastReleasePayQryTime\":\"" + Format.DateTimeFormat.format(eachFront.lastReleasePayQryTime) + "\"");
                } else {
                    sbSub.append(",\"lastReleasePayQryTime\":\"\"");
                }

                if (eachFront.lastExecOtherTime != null) {
                    sbSub.append(",\"lastExecOtherTime\":\"" + Format.DateTimeFormat.format(eachFront.lastExecOtherTime) + "\"");
                } else {
                    sbSub.append(",\"lastExecOtherTime\":\"\"");
                }
                if (eachFront.lastReleaseOtherTime != null) {
                    sbSub.append(",\"lastReleaseOtherTime\":\"" + Format.DateTimeFormat.format(eachFront.lastReleaseOtherTime) + "\"");
                } else {
                    sbSub.append(",\"lastReleaseOtherTime\":\"\"");
                }

                sbSub.append(",\"corporation\":\"" + eachFront.corporation + "\"");
                sbSub.append(",\"group\":\"" + eachFront.group + "\"");
                sbSub.append(",\"tenant\":\"" + eachFront.tenant + "\"");

                sbSub.append("}");
                listSubJson.add(sbSub.toString());
            }
        }
        if (listSubJson.size() == 0) {
            sbJson.append("{");
            sbJson.append("\"urid\":\"-\"");
            sbJson.append(",\"code\":\"-\"");
            sbJson.append(",\"name\":\"-\"");
            sbJson.append(",\"directSys\":\"所有渠道\"");

            sbJson.append(",\"payConcurrent\":\"0\"");
            sbJson.append(",\"qryConcurrent\":\"0\"");
            sbJson.append(",\"corporation\":\"-\"");
            sbJson.append(",\"group\":\"-\"");
            sbJson.append(",\"tenant\":\"-\"");

            sbJson.append("}");
        } else {
            int[] arrIdx = SortHelper.bubbleSortRetIdxDesc(listOrderNo);
            for (int i = 0; i < arrIdx.length; i++) {
                if (i > 0) {
                    sbJson.append(",");
                }
                sbJson.append(listSubJson.get(arrIdx[i]));
            }
        }
        sbJson.append("]");
        sbJson.append("}");
        return sbJson.toString();
    }
}
